﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ElevatorProject
{

    public partial class Form1 : Form
    {
        int loc;
        int louCen = 1;
        String text = "1";
        bool isUp = false;
        bool isDown = false;
        bool isOpen = false;
        bool isClose = false;
        bool[] isWork = new bool[6];

        public static int Floor = 7;
        /// <summary>
        ///内面按钮 [层数] 0-关 1-开 [11]开门 [12]关门
        /// </summary>
        /// 
        public bool[] InsideBtnState = new bool[Floor];

        /// <summary>
        ///外边按钮 [层数,0下1上] 0-上 1-下
        /// </summary>
        public bool[,] OutsideBtnState = new bool[Floor, 2];
        
        public Form1()
        {
            InitializeComponent();
            this.timerOpen = new System.Windows.Forms.Timer(this.components);
            this.timerClose = new System.Windows.Forms.Timer(this.components);
            // 
            // timerOpen
            // 
            this.timerOpen.Tick += new System.EventHandler(this.timerOpen_Tick);
            // 
            // timerClose
            // 
            this.timerClose.Tick += new System.EventHandler(this.timerClose_Tick);
            // 
            // timerValidate
            // 
            this.timerValidate.Tick += new System.EventHandler(this.timerValidate_Tick);
            OutsideBtnState[1, 1] = false;
            OutsideBtnState[6, 0] = false;
        }

        //电梯上升
        private void timerUp_Tick(object sender, EventArgs e)
        {
            if (isOpen == false && isClose == false)
            {
                isUp = true;
                int location = panel5.Location.Y;
                location -= 2;
                louCen = 7 - loc / 48;
                if (louCen == 0)
                {
                    text = (louCen + 1).ToString();
                }
                else
                {
                    text = louCen.ToString();
                }
                if (location >= loc)
                {
                    label1.ForeColor = System.Drawing.Color.Red;
                    label11.ForeColor = System.Drawing.Color.Red;
                    label1.Refresh();
                    label11.Refresh();
                    panel5.SetBounds(panel5.Location.X, location, 128, 48);
                }
                else
                {
                    this.button4.ForeColor = System.Drawing.Color.Blue;
                    this.button4.Refresh();
                    label1.ForeColor = System.Drawing.Color.Blue;
                    label11.ForeColor = System.Drawing.Color.Blue;
                    label1.Refresh();
                    label11.Refresh();
                    label14.Text = text;
                    label15.Text = text;
                    label14.Refresh();
                    label15.Refresh();
                    isUp = false;
                    timerUp.Enabled = false;
                    timerOpen.Start();
                }
            }
        }

        //电梯下降
        private void timerDown_Tick(object sender, EventArgs e)
        {
            if ((isOpen == false && isClose == false))
            {
                isDown = true;
                int location = panel5.Location.Y;
                location += 2;
                louCen = 7 - loc / 48;
                if (louCen == 0)
                {
                    text = (louCen + 1).ToString();
                }
                else
                {
                    text = louCen.ToString();
                }
                if (location <= loc)
                {
                    label2.ForeColor = System.Drawing.Color.Red;
                    label12.ForeColor = System.Drawing.Color.Red;
                    label2.Refresh();
                    label12.Refresh();
                    panel5.SetBounds(panel5.Location.X, location, 128, 48);
                }
                else
                {
                    label2.ForeColor = System.Drawing.Color.Blue;
                    label12.ForeColor = System.Drawing.Color.Blue;
                    label2.Refresh();
                    label12.Refresh();
                    label14.Text = text;
                    label15.Text = text;
                    label14.Refresh();
                    label15.Refresh();
                    isDown = false;
                    timerDown.Enabled = false;
                    timerOpen.Start();
                }
            }
        }

        //电梯开门
        private void timerOpen_Tick(object sender, EventArgs e)
        {
            isClose = false;
            isOpen = true;
            int x1 = panel6.Location.X;
            int x2 = panel7.Location.X;
            x1 -= 2;
            x2 += 2;
            if ((x2 - x1) < 204)
            {
                panel6.SetBounds(x1, panel6.Location.Y, 67, 158);
                panel7.SetBounds(x2, panel7.Location.Y, 67, 158);
            }
            else
            {
                isOpen = false;
                this.button14.ForeColor = System.Drawing.Color.Blue;
                button14.Refresh();
                timerOpen.Enabled = false;
                timerClose.Start();
            }
        }

        //电梯关门
        private void timerClose_Tick(object sender, EventArgs e)
        {
            isOpen = false;
            isClose = true;
            int x1 = panel6.Location.X;
            int x2 = panel7.Location.X;
            x1 += 2;
            x2 -= 2;
            if((x2 - x1) > 67)
            {
                panel6.SetBounds(x1, panel6.Location.Y, 67, 158);
                panel7.SetBounds(x2, panel7.Location.Y, 67, 158);
            }
            else
            {
                this.button16.ForeColor = System.Drawing.Color.Blue;
                button16.Refresh();
                isClose = false;
                timerClose.Enabled = false;
            }
        }

        //判定电梯运行
        private void timerValidate_Tick(object sender, EventArgs e)
        {
            for (int j = 0; j <= Floor - 2; j++)
            {
                if (isWork[j])
                {
                    for (int i = 0; i <= Floor - 1; i++)
                    {
                        if (InsideBtnState[i] || OutsideBtnState[i, 0])
                        {
                            if (panel5.Location.Y >= (288 - i * 48))
                            {
                                loc = 288 - i * 48;
                                timerUp.Start();
                            }
                            InsideBtnState[i] = false;
                            OutsideBtnState[i, 0] = false;
                        }
                    }
                }
            }
            for (int k = 0; k < Floor - 1; k++)
            {
                if (isDown)
                {
                    for (int i = Floor - 1; i >= 0; i--)
                    {
                        if (InsideBtnState[i] || OutsideBtnState[i, 1])
                        {
                            if (panel5.Location.Y <= (288 - i * 48))
                            {
                                loc = 288 - i * 48;
                                timerDown.Start();
                            }
                            InsideBtnState[i] = false;
                            OutsideBtnState[i, 1] = false;
                        }
                    }
                }
            }
            ////if (isUp && isDown) 
            ////{
            //    if (InsideBtnState[louCen-1] || OutsideBtnState[louCen-1, 0] || OutsideBtnState[louCen-1, 1])
            //    {
            //        //isUp = false;
            //        //isDown = false;
            //        for (int i = 0; i <= Floor-1; i++)
            //        {
            //            if (InsideBtnState[i] || OutsideBtnState[i, 0] || OutsideBtnState[i, 1])
            //            {
            //                if (i > louCen)
            //                    isUp = true;

            //                if (i < louCen)
            //                    isDown = true;

            //                //if (isUp)
            //                //{
            //                //    isWork = true;
            //                //    isUp = true;
            //                //    isClose = true;
            //                //    InsideBtnState[louCen] = false;
            //                //    OutsideBtnState[louCen, 1] = false;
            //                //}
            //                //else if (isDown)
            //                //{
            //                //    isWork = true;
            //                //    isDown = true;
            //                //    isClose = true;
            //                //    InsideBtnState[louCen] = false;
            //                //    OutsideBtnState[louCen, 0] = false;
            //                //}
            //                //else
            //                //{
            //                //    isWork = false;
            //                //    isUp = false;
            //                //    isDown = false;
            //                //    isOpen = true;

            //                //    InsideBtnState[louCen] = false;
            //                //    OutsideBtnState[louCen, 1] = false;
            //                //    OutsideBtnState[louCen, 0] = false;
            //                //}
                                //if (panel5.Location.Y >= (288 - (i + 1) * 48))
                                //{
                                //    //this.button4.ForeColor = System.Drawing.Color.Red;
                                //    //this.button4.Refresh();
                                //    loc = 288 - (i + 1) * 48;
                                //    timerUp.Start();
                                //}
                                //else
                                //{
                                //    //this.button4.ForeColor = System.Drawing.Color.Red;
                                //    //this.button4.Refresh();
                                //    loc = 288 - (louCen - 1) * 48;
                                //    timerDown.Start();
                                //}
            //            }
            //        }
            //    }
            //    else
            //    {

            //        isUp = false;
            //        isDown = false;

            //        for (int i = 0; i <= Floor-1; i++)
            //        {
            //            if (InsideBtnState[i] || OutsideBtnState[i, 0] || OutsideBtnState[i, 1])
            //            {
            //                if (i > louCen)
            //                    isUp = true;
            //                if (i < louCen)
            //                    isDown = true;
            //            }
            //        }
            //        //if (isUp)
            //        //{
            //        //    isWork = true;
            //        //    isUp = true;
            //        //    isClose = true;
            //        //}
            //        //else if (isDown)
            //        //{
            //        //    isWork = true;
            //        //    isDown = true;
            //        //    isOpen = false;
            //        //    isClose = false;
            //        //}
            //    }
            //    return;
            ////}
        }

        //退出程序
        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //关于
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("详情请咨询2008081080");
        }

        //第一层上按钮
        private void button3_Click(object sender, EventArgs e)
        {
            isDown = true;
            OutsideBtnState[0, 0] = true;
            timerValidate.Start();
            //if (panel5.Location.Y == 288)
            //{
            //    label14.Refresh();
            //    label15.Refresh();
            //    loc = 288;
            //    timerUp.Start();
            //    this.button3.ForeColor = System.Drawing.Color.Red;
            //    this.button3.Refresh();
            //    this.button3.ForeColor = System.Drawing.Color.Red;
            //    this.button3.Refresh();
            //}
            //else 
            //{
            //    this.button3.ForeColor = System.Drawing.Color.Blue;
            //    this.button3.Refresh();
            //    loc = 288;
            //    timerDown.Start();
            //    this.button3.ForeColor = System.Drawing.Color.Blue;
            //    this.button3.Refresh();
            //}         
        }

        //第二层上按钮
        private void button4_Click(object sender, EventArgs e)
        {
            isWork[0] = true;
            OutsideBtnState[1, 0] = true;            
        }

        //第二层下按钮
        private void button5_Click(object sender, EventArgs e)
        {
            isDown = true;
            OutsideBtnState[1, 1] = true;
            //if (panel5.Location.Y >=240)
            //{
            //    this.button5.ForeColor = System.Drawing.Color.Red;
            //    this.button5.Refresh();
            //    loc = 240;
            //    timerUp.Start();
            //    this.button5.ForeColor = System.Drawing.Color.Blue;
            //    this.button5.Refresh();
            //}
            //else
            //{
            //    this.button5.ForeColor = System.Drawing.Color.Red;
            //    this.button5.Refresh();
            //    loc = 240;
            //    timerDown.Start();
            //    this.button5.ForeColor = System.Drawing.Color.Blue;
            //    this.button5.Refresh();
            //}
        }

        //第三层上按钮
        private void button6_Click(object sender, EventArgs e)
        {
            isWork[1] = true;
            OutsideBtnState[2, 0] = true;
            //if (panel5.Location.Y >= 192)
            //{
            //    this.button6.ForeColor = System.Drawing.Color.Red;
            //    this.button6.Refresh();
            //    loc = 192;
            //    timerUp.Start();
            //    this.button6.ForeColor = System.Drawing.Color.Blue;
            //    this.button6.Refresh();
            //}
            //else
            //{
            //    this.button6.ForeColor = System.Drawing.Color.Red;
            //    this.button6.Refresh();
            //    loc = 192;
            //    timerDown.Start();
            //    this.button6.ForeColor = System.Drawing.Color.Blue;
            //    this.button6.Refresh();
            //}
        }

        //第三层下按钮
        private void button7_Click(object sender, EventArgs e)
        {
            isDown = true;
            OutsideBtnState[2, 1] = true;
            //if (panel5.Location.Y >= 192)
            //{
            //    this.button7.ForeColor = System.Drawing.Color.Red;
            //    this.button7.Refresh();
            //    loc = 192;
            //    timerUp.Start();
            //    this.button7.ForeColor = System.Drawing.Color.Blue;
            //    this.button7.Refresh();
            //}
            //else
            //{
            //    this.button7.ForeColor = System.Drawing.Color.Red;
            //    this.button7.Refresh();
            //    loc = 192;
            //    timerDown.Start();
            //    this.button7.ForeColor = System.Drawing.Color.Blue;
            //    this.button7.Refresh();
            //}
        }

        //第四层上按钮
        private void button8_Click(object sender, EventArgs e)
        {
            isWork[2] = true;
            OutsideBtnState[3,0] = true;
            //if (panel5.Location.Y >= 144)
            //{
            //    this.button8.ForeColor = System.Drawing.Color.Red;
            //    this.button8.Refresh();
            //    loc = 144;
            //    timerUp.Start();
            //    this.button8.ForeColor = System.Drawing.Color.Blue;
            //    this.button8.Refresh();
            //}
            //else
            //{
            //    this.button8.ForeColor = System.Drawing.Color.Red;
            //    this.button8.Refresh();
            //    loc = 144;
            //    timerDown.Start();
            //    this.button8.ForeColor = System.Drawing.Color.Blue;
            //    this.button8.Refresh();
            //}
        }

        //第四层下按钮
        private void button9_Click(object sender, EventArgs e)
        {
            isDown = true;
            OutsideBtnState[3, 1] = true;
            //if (panel5.Location.Y >=144)
            //{
            //    this.button9.ForeColor = System.Drawing.Color.Red;
            //    this.button9.Refresh();
            //    loc = 144;
            //    timerUp.Start(); ;
            //    this.button9.ForeColor = System.Drawing.Color.Blue;
            //    this.button9.Refresh();
            //}
            //else
            //{
            //    this.button9.ForeColor = System.Drawing.Color.Red;
            //    this.button9.Refresh();
            //    loc = 144;
            //    timerDown.Start();
            //    this.button9.ForeColor = System.Drawing.Color.Blue;
            //    this.button9.Refresh();
            //}
        }

        //第五层上按钮
        private void button10_Click(object sender, EventArgs e)
        {
            isWork[3] = true;
            OutsideBtnState[4, 0] = true;
            //if (panel5.Location.Y >= 96)
            //{
            //    this.button10.ForeColor = System.Drawing.Color.Red;
            //    this.button10.Refresh();
            //    loc = 96;
            //    timerUp.Start();
            //    this.button10.ForeColor = System.Drawing.Color.Blue;
            //    this.button10.Refresh();
            //}
            //else
            //{
            //    this.button10.ForeColor = System.Drawing.Color.Red;
            //    this.button10.Refresh();
            //    loc = 96;
            //    timerDown.Start();
            //    this.button11.ForeColor = System.Drawing.Color.Blue;
            //    this.button11.Refresh();
            //}
        }

        //第五层下按钮
        private void button11_Click(object sender, EventArgs e)
        {
            isDown = true;
            OutsideBtnState[4, 1] = true;
            //if (panel5.Location.Y >= 96)
            //{
            //    this.button11.ForeColor = System.Drawing.Color.Red;
            //    this.button11.Refresh();
            //    loc = 96;
            //    timerUp.Start();
            //    this.button11.ForeColor = System.Drawing.Color.Blue;
            //    this.button11.Refresh();
            //}
            //else
            //{
            //    this.button11.ForeColor = System.Drawing.Color.Red;
            //    this.button11.Refresh();
            //    loc = 96;
            //    timerDown.Start();
            //    this.button11.ForeColor = System.Drawing.Color.Blue;
            //    this.button11.Refresh();
            //}
        }

        //第六层上按钮
        private void button12_Click(object sender, EventArgs e)
        {
            isWork[4] = true;
            OutsideBtnState[5, 0] = true;
            //if (panel5.Location.Y >= 48)
            //{
            //    this.button12.ForeColor = System.Drawing.Color.Red;
            //    this.button12.Refresh();
            //    loc = 48;
            //    timerUp.Start();
            //    this.button12.ForeColor = System.Drawing.Color.Blue;
            //    this.button12.Refresh();
            //}
            //else
            //{
            //    this.button12.ForeColor = System.Drawing.Color.Red;
            //    this.button12.Refresh();
            //    loc = 48;
            //    timerDown.Start();
            //    this.button12.ForeColor = System.Drawing.Color.Blue;
            //    this.button12.Refresh();
            //}
        }
        
        //第六层下按钮
        private void button13_Click(object sender, EventArgs e)
        {
            isDown = true;
            OutsideBtnState[5, 1] = true;
            //if (panel5.Location.Y >= 48)
            //{
            //    this.button13.ForeColor = System.Drawing.Color.Red;
            //    this.button13.Refresh();
            //    loc = 48;
            //    timerUp.Start();
            //    this.button13.ForeColor = System.Drawing.Color.Blue;
            //    this.button13.Refresh();
            //}
            //else
            //{
            //    this.button13.ForeColor = System.Drawing.Color.Red;
            //    this.button13.Refresh();
            //    loc = 48;
            //    timerDown.Start();
            //    this.button13.ForeColor = System.Drawing.Color.Blue;
            //    this.button13.Refresh();
            //}
        }

        //第七层下按钮
        private void button15_Click(object sender, EventArgs e)
        {
            isWork[5] = true;
            OutsideBtnState[6, 1] = true;
            //if (panel5.Location.Y == 0)
            //{
            //    loc = 0;
            //    label14.Refresh();
            //    label15.Refresh();
            //    this.button15.ForeColor = System.Drawing.Color.Red;
            //    this.button15.Refresh();
            //    this.button15.ForeColor = System.Drawing.Color.Blue;
            //    this.button15.Refresh();
            //}
            //else {
            //    this.button15.ForeColor = System.Drawing.Color.Red;
            //    this.button15.Refresh();
            //    loc = 0;
            //    timerUp.Start();
            //    this.button15.ForeColor = System.Drawing.Color.Blue;
            //    this.button15.Refresh();
            //}           
        }

        //电梯开门按钮
        private void button14_Click(object sender, EventArgs e)
        {
            if (isUp == false && isDown == false)
            {
                this.button16.ForeColor = System.Drawing.Color.Blue;
                button16.Refresh();
                this.button14.ForeColor = System.Drawing.Color.Red;
                button14.Refresh();
                if (timerClose.Enabled == true)
                    timerClose.Stop();
                timerOpen.Start();
            }
        }

        //电梯关门按钮
        private void button16_Click(object sender, EventArgs e)
        {
            if (isUp == false && isDown == false)
            {
                this.button14.ForeColor = System.Drawing.Color.Blue;
                button14.Refresh();
                this.button16.ForeColor = System.Drawing.Color.Red;
                button16.Refresh();
                if (timerOpen.Enabled == true)
                    timerOpen.Stop();
                timerClose.Start();
            }
        }

        //电梯内部7层按钮
        private void button17_Click(object sender, EventArgs e)
        {
                this.button17.ForeColor = System.Drawing.Color.Red;
                button17.Refresh();
                loc = 0;
                timerUp.Start();
                this.button17.ForeColor = System.Drawing.Color.Blue;
                button17.Refresh();
        }

        //电梯内部6层按钮
        private void button18_Click(object sender, EventArgs e)
        {
            if (panel5.Location.Y > 48)
            {
                this.button18.ForeColor = System.Drawing.Color.Red;
                button18.Refresh();
                loc = 48;
                timerUp.Start();
                this.button18.ForeColor = System.Drawing.Color.Blue;
                button18.Refresh();
            }
            else
            {
                this.button18.ForeColor = System.Drawing.Color.Red;
                button18.Refresh();
                loc = 48;
                timerDown.Start();
                this.button18.ForeColor = System.Drawing.Color.Blue;
                button18.Refresh();
            }
        }

        //电梯内部5层按钮
        private void button19_Click(object sender, EventArgs e)
        {
            if (panel5.Location.Y > 96)
            {
                this.button19.ForeColor = System.Drawing.Color.Red;
                button19.Refresh();
                loc = 96;
                timerUp.Start();
                this.button19.ForeColor = System.Drawing.Color.Blue;
                button19.Refresh();
            }
            else
            {
                this.button19.ForeColor = System.Drawing.Color.Red;
                button19.Refresh();
                loc = 96;
                timerDown.Start();
                this.button19.ForeColor = System.Drawing.Color.Blue;
                button19.Refresh();
            }
        }

        //电梯内部4层按钮
        private void button20_Click(object sender, EventArgs e)
        {
            if (panel5.Location.Y > 144)
            {
                this.button20.ForeColor = System.Drawing.Color.Red;
                button20.Refresh();
                loc = 144;
                timerUp.Start();
                this.button20.ForeColor = System.Drawing.Color.Blue;
                button20.Refresh();
            }
            else
            {
                this.button20.ForeColor = System.Drawing.Color.Red;
                button20.Refresh();
                loc = 144;
                timerDown.Start();
                this.button20.ForeColor = System.Drawing.Color.Blue;
                button20.Refresh();
            }
        }

        //电梯内部3层按钮
        private void button21_Click(object sender, EventArgs e)
        {
            if (panel5.Location.Y > 192)
            {
                this.button21.ForeColor = System.Drawing.Color.Red;
                button21.Refresh();
                loc = 192;
                timerUp.Start();
                this.button21.ForeColor = System.Drawing.Color.Blue;
                button21.Refresh();
            }
            else 
            {
                this.button21.ForeColor = System.Drawing.Color.Red;
                button21.Refresh();
                loc = 192;
                timerDown.Start();
                this.button21.ForeColor = System.Drawing.Color.Blue;
                button21.Refresh();
            }
        }

        //电梯内部2层按钮
        private void button22_Click(object sender, EventArgs e)
        {
            if (panel5.Location.Y > 240)
            {
                this.button22.ForeColor = System.Drawing.Color.Red;
                button22.Refresh();
                loc = 240;
                timerUp.Start();
                this.button22.ForeColor = System.Drawing.Color.Blue;
                button22.Refresh();
            }
            else
            {
                this.button22.ForeColor = System.Drawing.Color.Red;
                button22.Refresh();
                loc = 240;
                timerDown.Start();
                this.button22.ForeColor = System.Drawing.Color.Blue;
                button22.Refresh();
            }
        }

        //电梯内部1层按钮
        private void button23_Click(object sender, EventArgs e)
        {
            if (panel5.Location.Y <=288)
            {
                this.button23.ForeColor = System.Drawing.Color.Red;
                button23.Refresh();
                loc = 288;
                timerDown.Start();
                this.button23.ForeColor = System.Drawing.Color.Blue;
                button23.Refresh();
            }
        }

        private void button24_Click(object sender, EventArgs e)
        {
            this.button24.ForeColor = System.Drawing.Color.Red;
            button24.Refresh();
            timerValidate.Start();
        }

        private void panel7_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel5_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void panel6_Paint(object sender, PaintEventArgs e)
        {

        }
    }

}